<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        
        $this->display();
    }

    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类

        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
        // $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub=false;
        // 上传文件
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
           $this->error($upload->getError());
        }else{// 上传成功


            return $info;
           //$this->success('上传成功！');
        }
    }
    function img(){
       
           $photoinfo=$this->upload();
           $filepath='./Public/Uploads/photo/';
           $oldfilename=$photoinfo['photo']['savename'];
           $oldfileFullname=$filepath.'\\'.$oldfilename;
           $newfilename='thumb_'.$oldfilename;
           $newfileFullname=$filepath.'\\'.$newfilename;

           $image=new\Think\Image();
           $image->open($oldfileFullname);
           $image->thumb(100,100)->save($newfileFullname);
           return $newfilename;
        
    }
    
    public function register(){
    	//添加用户
    	//用户名是否存在，口令是否正确，口令加密
    	$userModel=D('Users');
    	if ($userModel->create())
    	 {
    	 	if($_FILES['photo']['error']==0)//判断文件是否上传成功
    	    {
              $fileName=$this->img();
             //$photoinfo=$this->upload();//将临时文件保存都指定位置
             $userModel->photo=$fileName;
            }

    		if ($userModel->add())
    		{
    			$this->success('注册成功',U('Admin/Index/users'));
    		}else{
    			$this->error('注册失败');
    		}
    	}else{
    		$this->error($userModel->getError());
    	}
    }
}